#include "base_file.h"

namespace base
{
namespace utils
{
BaseFile::BaseFile(const std::string &file_name)
{
  file_name_ = file_name;
}
bool BaseFile::exists(const std::string &filePath)
{
  return access(filePath.c_str(), F_OK) == 0;
}
bool BaseFile::checkFileMode(const std::string &filePath, base::utils::file::FileMode mode)
{
  return access(filePath.c_str(), (int) mode) == 0;
}
bool BaseFile::chmodFile(const std::string &filePath, int mode)
{
  return chmod(filePath.c_str(), mode) == 0;
}
BaseFile::~BaseFile()
{
  close();
}
bool BaseFile::open(file::OpenModeFlag mode)
{
  if (file_stream_.is_open())
  {
    close();
  }
  file_stream_.open(file_name_, (std::ios_base::openmode) mode);
  return file_stream_.is_open();
}
bool BaseFile::open(std::ios_base::openmode mode)
{
  if (file_stream_.is_open())
  {
    close();
  }
  file_stream_.open(file_name_, mode);
  return file_stream_.is_open();
}
void BaseFile::close()
{
  if (file_stream_.is_open())
  {
    file_stream_.close();
  }
}
void BaseFile::seekInputPosition(std::size_t offsetPosition, std::ios_base::seekdir position)
{
  file_stream_.seekg(offsetPosition, position);
}
bool BaseFile::endOfFile() const
{
  return file_stream_.eof();
}
std::string BaseFile::getFileName() const
{
  return file_name_;
}
}
}
